package com.txd.mofa.wode.aty;


import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.utils.NameLengthFilter;
import com.txd.mofa.utils.TextviewJiazaiHtml;

import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Layout(R.layout.aty_nicheng)
public class NichengAty extends BaseAty {

    private ImageView iv_back;
    private EditText et;
    private TextView tv_baocun;

    private int MIN_lENTH = 4;

    @Override
    public void initViews() {
        super.initViews();

        iv_back = findViewById(R.id.iv_back);
        et = findViewById(R.id.et);
        tv_baocun = findViewById(R.id.tv_baocun);

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                InputFilter[] filters = {new NameLengthFilter(16)};
                et.setFilters(filters);

                if (s.length() != 0 && s != null) {

                    if (getChineseCount(s.toString()) == 1) {
                        etLenth = 2;
                        if (s.length() > 1) {
                            etLenth = etLenth+s.length() - 1;
                        }
                    } else if (getChineseCount(s.toString()) >= 2){
                        etLenth = getChineseCount(s.toString()) * 2;
                    }else {
                        etLenth = s.length();
                    }



                    if (etLenth >= 4) {
                        tv_baocun.setBackground(getResources().getDrawable(R.mipmap.button_huang));
                    } else {
                        tv_baocun.setBackground(getResources().getDrawable(R.mipmap.button_hei));
                    }
                } else {
                    tv_baocun.setBackground(getResources().getDrawable(R.mipmap.button_hei));
                }

                if (etLenth == 16){
                    Toast.makeText(me, "最多16个字符", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private int etLenth = 0;

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    private int getChineseCount(String str) {
        int count = 0;
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            for (int i = 0; i <= m.groupCount(); i++) {
                count = count + 1;
            }
        }
        return count;
    }

    String regEx = "[\\u4e00-\\u9fa5]";

    private boolean isChinest(String source) {
        return Pattern.matches(regEx, source);
    }

    @Override
    public void initDatas(JumpParameter paramer) {

        String nicheng = paramer.getString("nicheng");

        TextviewJiazaiHtml.setFuzhiText(et,nicheng);

//        SpannableString s = new SpannableString(nicheng);//这里输入自己想要的提示文字
//        et.setHint(s);
//        et.setHintTextColor(getResources().getColor(R.color.hui));
    }

    @Override
    public void setEvents() {

        tv_baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etLenth < 4) {
                    Toast.makeText(me, "最少四个字符", Toast.LENGTH_SHORT).show();
                }else {
                    if (et.getText().toString().equals("") || et.getText().toString() == null) {
                        Toast.makeText(me, "昵称不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        xiugai();
                    }
                }
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void xiugai() {
        HttpRequest.POST(me, modifyNickname, new Parameter()
                .add("token", token)
                .add("nickname", et.getText().toString()), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    Toast.makeText(me, map.get("message"), Toast.LENGTH_SHORT).show();
                    if (map.get("code").equals("1")) {
                        finish();
                    }
                } else {
                    toast("网络异常");
                }
            }
        });
    }
}
